package com.woke.fragment;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.Toast;

import com.example.woke.BorrowinfoActivity;
import com.example.woke.CreditinfoActivity;
import com.http.HttpMethods;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.JsonHttpResponseHandler;
import com.loopj.android.http.PersistentCookieStore;
import com.loopj.android.http.RequestParams;
import com.woke.adapter.Myloanadter;
import com.woke.data.Data_myloan;
import com.woke.method.Intentnet;
import com.woke.method.MyApp;
import com.woke.serizedatas.Datas_load;
import com.zhongcai.online.R;

import org.apache.http.Header;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;

public class Myloan1Fragment extends Fragment implements OnItemClickListener{
	
	private View layout;
	private LayoutInflater minflater;
	private ListView mListView;
//	private String getuserid = "";
//	private ImageView mBar;
	private String getuser_id;
	private MyApp myapp;
	private ArrayList<Data_myloan> datalist1 = new ArrayList<Data_myloan>();
	private ArrayList<Fragment> list = new ArrayList<Fragment>();
	private Myhotpreciver reciver;
	private Myloanadter adapter;
	private RelativeLayout mRelativ;
	private boolean makenew;//��������淵��ʱ�ж��Ƿ�ˢ��
	private Intentnet net;
	private Datas_load dataload;
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		
		if(layout == null){
			minflater = inflater;
			layout = minflater.inflate(R.layout.item_singel_listview, null);
			intview();
		}else{
			ViewGroup parent = (ViewGroup) layout.getParent();
			if(parent!=null){
			parent.removeView(layout);
			}
		}
		return layout;
	}
	
	
	class Myhotpreciver extends BroadcastReceiver{
		@Override
		public void onReceive(Context context, Intent intent) {
			
			Bundle extras = intent.getExtras();
			boolean sucessbor = extras.getBoolean("sucessbor", false);
			int parent = extras.getInt("parent", 101);
//			Log.e("��Ǯ�˳�", "ddd"+feyanUsersId);
//			int parent = extras.getInt("parent");
			if(parent != 101 && sucessbor == true){
				if(null != datalist1 && datalist1.size() >0){
					datalist1.remove(parent);
					adapter.notifyDataSetChanged();
				}
			}
				
		}
	}
	
	@Override
	public void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		net = new Intentnet();
		myapp = (MyApp) getActivity().getApplication();
		dataload = myapp.getDatas_load();
		if(null == dataload){
			Toast.makeText(getActivity(), "请先登录", Toast.LENGTH_SHORT).show();
		}else{
		getuser_id  = myapp.getDatas_load().getId();
			datalist1.clear();
			getmyloandata1();
		}
	}

	@Override
	public void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		getActivity().unregisterReceiver(reciver);
		
	}
	
	private void intview() {
		reciver = new Myhotpreciver();
		IntentFilter filter = new IntentFilter();
		filter.addAction("com.woke.personborrowinfoavtivity");
		getActivity().registerReceiver(reciver, filter);
		
//		mBar = (ImageView) layout.findViewById(R.id.item_singerl_bar);
//		mBar.startAnimation(
//				AnimationUtils.loadAnimation(getActivity(), R.anim.btn_rotate));
		mListView = (ListView) layout.findViewById(R.id.listview_singel);
		mListView.setOnItemClickListener(this);
		
		mRelativ = (RelativeLayout) layout.findViewById(R.id.fmmyloan_frame);
		
		
	}

	private void getmyloandata1() {
		AsyncHttpClient ahc = new AsyncHttpClient();
		PersistentCookieStore myCookieStore = new PersistentCookieStore(getActivity());    
        ahc.setCookieStore(myCookieStore); 
		RequestParams params = new RequestParams();
		params.put("g", "api");
		params.put("m", "quota");
		params.put("a", "getPersonFlag");
		params.put("user_id", getuser_id);
		ahc.get(HttpMethods.BASEURL, params, new JsonHttpResponseHandler(){
			@Override
			public void onSuccess(int statusCode, Header[] headers,
					JSONObject response) {
				super.onSuccess(statusCode, headers, response);
				Log.e("我的99贷款", ""+response);
				try {
					String flag = response.getString("flag");
					if(flag.equals("success")){
						JSONObject jsonObject = response.getJSONObject("data");
						JSONArray arra = jsonObject.getJSONArray("data");
						for (int i = 0; i < arra.length(); i++) {
							JSONObject data = arra.getJSONObject(i);
							String id = data.getString("id");
							String user_id = data.getString("user_id");
							String type = data.getString("type");
							String interest = data.getString("interest");
							String quota_money = data.getString("quota_money");
							String quota_use = data.getString("quota_use");
							String services_fee = data.getString("services_fee");
							String repay_date = data.getString("repay_date");
							String creat_date = data.getString("creat_date");
							String status = data.getString("status");
							String mark = data.getString("mark");
							String exp_date = data.getString("exp_date");
							String quota_type = data.getString("quota_type");
							String friend = data.getString("friend");
							String quota_pic = data.getString("quota_pic");
							String quota_pic1 = data.getString("quota_pic1");
							String quota_pic2 = data.getString("quota_pic2");
							String flag1 = data.getString("flag");
//							String user_nicename = data.getString("user_nicename");
//							String avatar = data.getString("avatar");
//							String money = data.getString("money");
							Data_myloan list = new Data_myloan(id,user_id,type,interest
									,quota_money,quota_use,services_fee, 
									repay_date,creat_date, status,mark,
									exp_date, quota_type,
									friend,quota_pic,quota_pic1,quota_pic2,flag1);
							datalist1.add(list);
						}
					}
				} catch (JSONException e) {
					e.printStackTrace();
				}
				
				if(datalist1.size() > 0 && makenew == false){
					adapter = new Myloanadter(minflater,datalist1,getuser_id);
					mListView.setAdapter(adapter);
				}else if(datalist1.size() > 0 && makenew == true){
					adapter.notifyDataSetChanged();
				}
				
			}
			
			@Override
			public void onFailure(int statusCode, Header[] headers,
					String responseString, Throwable throwable) {
				super.onFailure(statusCode, headers, responseString, throwable);
				Toast.makeText(getActivity(), "数据获取失败！", Toast.LENGTH_SHORT).show();
//				Log.e("response", "ʧ��"+responseString);
			}
			
			@Override
			public void onStart() {
				// TODO Auto-generated method stub
				super.onStart();
				mRelativ.setVisibility(View.VISIBLE);
			}
			
			@Override
			public void onFinish() {
				super.onFinish();
//				mBar.clearAnimation();
				mRelativ.setVisibility(View.GONE);
//				Log.e("response", "���");
			}
		});
	}
	
	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
		if(net.isNetworkConnected(getActivity()) == false){
			Toast.makeText(getActivity(), "请检查您的网络！", Toast.LENGTH_SHORT).show();
		}else{
		if(!datalist1.get(arg2).type.equals("1")){
			Intent intent = new Intent(getActivity(),CreditinfoActivity.class);
			intent.putExtra("id", datalist1.get(arg2).id);
			intent.putExtra("user_id", datalist1.get(arg2).user_id);
			startActivity(intent);
		}else{
			Intent intent = new Intent(getActivity(),BorrowinfoActivity.class);
			intent.putExtra("badboy", true);
			intent.putExtra("id", datalist1.get(arg2).id);
			if(datalist1.get(arg2).flag1.equals("2")){//2Ϊ��� 1����
				intent.putExtra("currno", 102);//�ж��Ƿ��ǽ��뻹�ǽ��
			}else{
				intent.putExtra("currno", arg2);
			}
			String user_id = datalist1.get(arg2).user_id;
			String status = datalist1.get(arg2).status;
			String flag1 = datalist1.get(arg2).flag1;
			
			if(status.endsWith("1") || status.endsWith("2") || status.endsWith("3")){
				myapp.sethavetopay("14");//��ʱ���Բ�����
			}else if(status.endsWith("4") && user_id.endsWith(dataload.getId())){//id�����Լ���idʱ��Ҫ��Ǯ
				myapp.sethavetopay("13");//Ҫ��Ǯ
			}else if(status.endsWith("4") && !user_id.endsWith(dataload.getId())){//id�������Լ���idʱ�ȴ�Է���Ǯ
				myapp.sethavetopay("10");//�ȴ�Է���Ǯ
			}else if(status.endsWith("7") && user_id.endsWith(dataload.getId())){//id�����Լ���idʱ��Ҫ��Ǯ
				myapp.sethavetopay("13");//Ҫ��Ǯ��������
			}else if(status.endsWith("6")){
				myapp.sethavetopay("12");//�������
			}else if(status.endsWith("7") && !user_id.endsWith(dataload.getId())){
				myapp.sethavetopay("10");//�Է����ڣ��ȴ�Է���Ǯ
			}
			startActivity(intent);		
		}
	}
	}

}
